package net.mihome.app.ui;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;


import com.loopj.android.http.RequestParams;

import net.mihome.app.AppContext;
import net.mihome.app.R;
import net.mihome.app.adapter.RssNewsAdapter;
import net.mihome.app.bean.RssNews;
import net.mihome.app.bean.URLs;
import net.mihome.app.bean.net.ResponsePojo;
import net.mihome.app.utils.MiAsyncHttpResponseHandler;
import net.mihome.app.utils.RequestParamsFactory;
import net.mihome.app.widget.PullToRefreshListView;

import org.apache.http.Header;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import me.maxwin.view.XListView;

/**
 * Created by ZHAO on 2014/12/29 0029.
 */
public class RssTypeFragment extends Fragment {
    static  RssTypeFragment instance;
    private List<RssNews> listNews=new ArrayList<RssNews>();
    private RssNewsAdapter rssNewsAdapter;
    private XListView newsListView;
    private Handler handler;
    private int begin=0;
    private int num=10;
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View mainView= inflater.inflate(R.layout.frame_rss,null,false);
        newsListView=(XListView) mainView.findViewById(R.id.rss_list_news_lv);
        rssNewsAdapter=new RssNewsAdapter(getActivity(),listNews);
        newsListView.setAdapter(rssNewsAdapter);
        newsListView.setPullLoadEnable(true);
        newsListView.setPullRefreshEnable(true);
        newsListView.setXListViewListener(new XListView.IXListViewListener() {
            @Override
            public void onRefresh() {
                initNewsData(AddType.PRE);
            }

            @Override
            public void onLoadMore() {
                initNewsData(AddType.NEXT);
            }
        });
        initNewsData(AddType.CLEAR);
        return mainView;
    }
    public static RssTypeFragment newInstance(){
        if(instance==null){
            instance=new RssTypeFragment();
        }
        return instance;
    }

    /**
     * 上一页
     * */
    public void prePage(){
        if(begin>num){
            begin=begin-num;
        }else{
            begin=0;
        }
        initNewsData(AddType.PRE);
    }
    /**
     * 下一页s
     * */
    public void nextPage(){
        begin=begin+10;
        initNewsData(AddType.NEXT);
    }
    enum AddType{
        PRE,NEXT,CLEAR
    }
      /***
     * 线程获取数据
     **/
    public void initNewsData(final AddType addType){
        RequestParams requestParams= RequestParamsFactory.newInstance(AppContext.getInstance());
        requestParams.put("begin",begin);
        requestParams.put("num",num);
        AppContext.ASYNC_HTTP_CLIENT.get(getActivity(), URLs.MIHOME_GET_RSSTYPE_LIST_RADOM,requestParams,new MiAsyncHttpResponseHandler<List<Map>>() {
            @Override
            public void onSuccess(int arg0, Header[] arg1, ResponsePojo<List<Map>> responsePojo) {
                List<RssNews> rssNewses=RssNews.parseList(responsePojo.getData());
                if(addType==AddType.CLEAR){
                    listNews.clear();
                    listNews.addAll(rssNewses);
                }else if(addType==AddType.NEXT){
                    listNews.addAll(rssNewses);
                }else if(addType==AddType.PRE){
                    listNews.addAll(0,rssNewses);
                }
                rssNewsAdapter.notifyDataSetChanged();
                newsListView.stopLoadMore();
                newsListView.stopRefresh();
            }

            @Override
            public void onFailure(int arg0, Header[] arg1, Throwable arg3) {
                Toast.makeText(getActivity(),"请检查网络",Toast.LENGTH_SHORT).show();
                newsListView.stopLoadMore();
                newsListView.stopRefresh();
            }
        });
    }
 }
